<?php
namespace app\index\controller;
header('Access-Control-Allow-Origin:*');
use app\common\controller\HomeBase;
use think\Db;
use think\Model;
class MyCenter extends HomeBase
{
	//用户
	protected $user_model;
	//帖子
	protected $article_model;
	//积分
	protected $integral_model;
	//回帖
	protected $reply_model;
	//跟帖
	protected $after_model;
	//签到
	protected $signin_model;
	//签到记录
	protected $signmanage_model;
	//用户收藏
	protected $usercollect_model;
	//二手车
	protected $usedtruck_model;
	//用户优惠券
	protected $usercoupon_model;
	//购物券
	protected $coupon_model;
	//用户卡片
	protected $kapian_model;
	//导航
	protected $nav_model;
	//导航
	protected $userfeedbacks_model;
 	protected function _initialize(){
        parent::_initialize();
        $this->user_model = model('user');
        $this->article_model = model('article');
        $this->integral_model = model('integral');
        $this->reply_model = model('reply');
        $this->after_model = model('after');
        $this->signin_model = model('signin');
        $this->signmanage_model = model('sign_manage');
        $this->usercollect_model = model('user_collect');
        $this->usedtruck_model = model('used_truck');
        $this->usercoupon_model = model('usercoupon');
        $this->coupon_model = model('coupon');
        $this->kapian_model = model('kapian');
        $this->nav_model = model('nav');
        $this->userfeedbacks_model = model('userfeedbacks');
    }
    /**
     * 个人中心主页
     * @param  string $id [description]
     * @return [type]     [description]
     */
    public function index($id = ''){
    	if($id){
    		//用户信息
    		$user_find = $this->user_model->field(['name','headImg','vip'])->where('status',1)->where('id',$id)->find();
    		//帖子总数
    		$article_list = $this->article_model->where('uid',$id)->select();
    		//积分
    		$jifen = $this->integral_model->where('userID',$id)->value('userIntegral');
    		//发帖点赞
    		$article_likes = 0;
    		foreach( $article_list as $i ){
    			$article_likes += $i['likes'];
    		}
    		//回帖点赞
    		$reply_likes = $this->reply_model->where('user_id',$id)->sum('likes');
    		//点赞总数
    		$likes = $article_likes + $reply_likes;
    		//返回信息
    		$myCenter['name'] = $user_find['name'];
    		$myCenter['img'] = $user_find['headImg'];
    		$myCenter['vip'] = $user_find['vip'];
    		$myCenter['article_count'] = count($article_list);
    		$myCenter['jifen'] = $jifen;
    		$myCenter['likes'] = $likes;
    		$this->apiSuccess($myCenter);
    	}else{
			$this->apiError('请求错误');
		}
    }
    /**
     * 用户签到
     * @param  string $id [description]
     * @return [type]     [description]
     */
    public function qiandao($id = '')
    {
    	if($id){
            //是否签到
            $myCenter['sign'] = 0;
    		//获取最后一次签到时间
            if($signmanage_date = $this->signmanage_model->where('user_id',$id)->order('id DESC')->value('create_time')){
                $today = date('Y-m-d',time());
                //判断是否签到
                if(strtotime($today) == strtotime(date('Y-m-d',strtotime($signmanage_date)))){
                    $myCenter['sign'] = 1;
                }
            }

    		//用户信息
    		$user_find = $this->user_model->field(['name','headImg','vip'])->where('status',1)->where('id',$id)->find();
    		//获取总积分
    		$jifen = $this->integral_model->where('userID',$id)->value('userIntegral');
    		//获取所有签到时间
            $signmanage_column = $this->signmanage_model->where('user_id',$id)->order('create_time')->column('create_time');
    		if($signmanage_column = $this->signmanage_model->where('user_id',$id)->order('create_time')->column('create_time')){
                foreach ($signmanage_column as $key => $value) {
                    $value = date('Y-m-d',strtotime($value));
                    $uuzu  = explode('-', $value);
                    $myCenter['qiandao_time'][$key] = $uuzu['2'];
                }
            }
    		//返回信息
            $myCenter['jifen'] = $jifen;
    		$myCenter['date_time'] = date("Y-m-d");

    		$this->apiSuccess($myCenter);
    	}else{
			$this->apiError('请求错误');
		}
    }
    /**
     * 用户签到信息更新
     * @param  string $id [description]
     * @return [type]     [description]
     */
    public function qiandao_update($id = '')
    {
    	if($id){
    		//用户信息
    		$user_find = $this->user_model->field(['name','headImg','vip'])->where('status',1)->where('id',$id)->find();
    		//获取一次签到积分数
    		if($user_find['vip']){
    			$signin = $this->signin_model->value('signinVip');
    		}else{
    			$signin = $this->signin_model->value('signinIntegral');
    		}
    		//用户积分更新
    		$jifen_update = $this->integral_model->where('userID',$id)->setInc('userIntegral',$signin);
    		//添加签到信息
    		$signmanage_data['user_id'] = $id;
    		$signmanage_data['integral'] = $signin;
    		$signmanage_insert = $this->signmanage_model->save($signmanage_data);
    		//积分
    		$jifen = $this->integral_model->where('userID',$id)->value('userIntegral');
			//返回信息
    		$myCenter['jifen'] = $jifen;
    		$this->apiSuccess($myCenter);
    	}else{
			$this->apiError('请求错误');
		}
    }
    /**
     * 我的帖子
     * @param  string  $id        [description]
     * @param  integer $page      [description]
     * @param  integer $page_size [description]
     * @return [type]             [description]
     */
    public function my_tiezi($id = '',$page = 1,$page_size = 5)
    {
    	if($id){
    		//用户信息
    		$user_find = $this->user_model->field(['name','headImg','vip'])->where('status',1)->where('id',$id)->find();
    		//帖子列表
    		$article_list = $this->article_model->field(['id','title','photo','create_time','reply','likes','status'])->where('uid',$id)->order('create_time DESC')->page($page,$page_size)->select();
    		foreach($article_list as $key => $al){
    			$article[$key] = $al->toArray();
    		}
			//返回信息
    		$myCenter['name'] = $user_find['name'];
    		$myCenter['img'] = $user_find['headImg'];
    		$myCenter['vip'] = $user_find['vip'];
            $myCenter['article_count'] = count($article);
            $myCenter['article'] = $article;

    		$this->apiSuccess($myCenter);
    	}else{
			$this->apiError('请求错误');
		}
    }
    /**
     * 用户消息
     * @param  string  $id        [description]
     * @param  integer $page      [description]
     * @param  integer $page_size [description]
     * @return [type]             [description]
     */
    public function xiaoxi($id = '',$page = 1,$page_size = 5)
    {
    	if($id){
    		//已查看
    		$reading_yes = [];
    		//未查看
    		$reading_no = [];
    		//发送人信息
    		$send_list_id = [];
    		//id为索引的所有用户信息
    		$user_data = [];
    		//用户信息
    		$user_list = $this->user_model->field(['id','name','headImg','vip'])->where('status',1)->select();
    		//将所有用户信息以ID为索引的形式存储
    		foreach($user_list as $ulist){
    			$user_data[$ulist['id']] = $ulist;
    		}
    		//查询跟帖
    		$after_list = $this->after_model->where('user_id',$id)->order('create_time')->select();
    		foreach($after_list as $key => $al){
    			//存储所有发送人ID
    			array_unshift($send_list_id,$al['after_userid']);
    			//查询并存储发送人信息
    			$al['user_info'] = $user_data[$al['after_userid']];
    			if($al['reading'] == 1){
    				array_unshift($reading_yes,$al->toArray());
    			}else{
    				array_unshift($reading_no,$al->toArray());
    			}
    		}
    		//查询用户全部帖子
    		$article_list = $this->article_model->where('status',1)->where('uid',$id)->column('id');
    		//查询所有回帖
    		$reply_list = $this->after_model->order('create_time')->select();
    		foreach($article_list as $key => $ul){
    			foreach($reply_list as $index => $li){
    				if($li['article_id'] == $ul['id']){
    					//存储所有发送人ID
    					array_unshift($send_list_id,$li['user_id']);
    					//查询并存储发送人信息
    					$li['user_info'] = $user_data[$li['user_id']];
    					if($li['reading'] == 1){
    						array_unshift($reading_yes,$li->toArray());
    					}else{
    						array_unshift($reading_no,$li->toArray());
    					}
    				}
    			}
    		}
			//返回信息
    		$myCenter['reading_yes'] = $reading_yes;
    		$myCenter['reading_no'] = $reading_no;
    		$this->apiSuccess($myCenter);
    	}else{
			$this->apiError('请求错误');
		}
    }
    /**
     * 用户收藏
     * @param  string  $id        [description]
     * @param  integer $page      [description]
     * @param  integer $page_size [description]
     * @param  integer $fenlei    [1帖子,2二手车]
     * @return [type]             [description]
     */
    public function shoucang($id = '',$page = 1,$page_size = 5,$fenlei = 1)
    {
    	if($id){
    		//返回数据
    		$myCenter = [];
    		//用户信息
    		$user_list = $this->user_model->field(['id','name','headImg','vip'])->where('status',1)->select();
    		//将所有用户信息以ID为索引的形式存储
    		foreach($user_list as $ulist){
    			$user_data[$ulist['id']] = $ulist;
    		}
    		//用户收藏信息
    		$usercollect = $this->usercollect_model->where('user_id',$id)->find();
    		//帖子信息
    		$article_list = $this->article_model->field(['id','uid','title','photo','create_time','reply','likes'])->where('status',1)->select();
    		//二手车
    		$usedtruck_list = $this->usedtruck_model->field(['id','user_id','title','photo','price'])->where('status',1)->select();
    		//收藏帖子
    		if($fenlei == 1){
    			foreach($article_list as $al){
    				if($al['id'] == $usercollect['article_id']){
    					$al['user_info'] = $user_data[$al['uid']];
    					//返回信息
    					array_unshift($myCenter,$al->toArray());
    				}
    			}
    		}
    		//收藏二手车
    		if($fenlei == 2){
    			foreach($usedtruck_list as $ul){
    				if($ul['id'] == $usercollect['used_truck_id']){
    					//返回信息
    					array_unshift($myCenter,$ul->toArray());
    				}
    			}
    		}
    		$this->apiSuccess($myCenter);
    	}else{
			$this->apiError('请求错误');
		}
    }
    /**
     * 用户优惠券
     * @param  string  $id        [description]
     * @param  integer $page      [description]
     * @param  integer $page_size [description]
     * @return [type]             [description]
     */
    public function usercoupon($id = '',$page = 1,$page_size = 10)
    {
		if($id){
			//返回数据
    		$myCenter = [];
			//用户优惠券
			$usercoupon_list = $this->usercoupon_model
									->field(['couponID','id','coding','buyDate','offsetMoney'])
									->where('status',1)
									->where('uses',1)
									->where('userID',$id)
									->select();
			//购物券
			$coupon = $this->coupon_model->where('status',1)->column('period','id');
			//判断用户优惠券是否过期
			$new_date = strtotime(date('Y-m-d H:i:s'));
			foreach($usercoupon_list as $ul){
				$usercoupon_date =  strtotime($ul['buyDate']);
				$chazhi = ceil(($new_date - $usercoupon_date)/86400); //60s*60min*24h
				if($chazhi > $coupon[$ul['couponID']]){
					//用户优惠券过期处理
					$this->usercoupon_model->where('id',$ul['id'])->save(['status'=>'0']);
				}else{
					//返回未过期优惠券数据
					//过期时间的unix
					$coupon_unix = ($coupon[$ul['couponID']]*86400);
					//获取过期日期
					$guoqi_date = date('Y-m-d H:i:s',($usercoupon_date + $coupon_unix));
					$ul['guoqi_date'] = $guoqi_date;
					//截取信息
					$cun = $ul->toArray();
					array_splice($cun,0,2);
					//返回信息
    				array_unshift($myCenter,$cun);
				}
			}
			$this->apiSuccess($myCenter);
		}else{
			$this->apiError('请求错误');
		}
    }
    /**
     * 用户卡包
     * @param  string  $id        [description]
     * @param  integer $page      [description]
     * @param  integer $page_size [description]
     * @return [type]             [description]
     */
    public function kapian($id = '',$page = 1,$page_size = 5)
    {
		if($id){
			//返回数据
    		$myCenter = [];
			//用户优惠券
			$kapian_list = $this->kapian_model
									->field(['id','nav_id','card_number'])
									->where('user_id',$id)
									->select();
			//购物券
			$nav = $this->nav_model->where('status',1)->column('name','id');
			foreach($kapian_list as $kl){
				$nav_name = $nav[$kl['nav_id']];
				$kl['name'] = $nav_name;
				//截取信息
				$cun = $kl->toArray();
				array_splice($cun,0,2);
				//返回信息
    			array_unshift($myCenter,$cun);
			}
			$this->apiSuccess($myCenter);
		}else{
			$this->apiError('请求错误');
		}
    }
    /**
     * 关于我们
     */
    public function we()
    {
    		//站点配置
    		$site_config = Db::name('system')->field('value')->where('name', 'site_config')->find();
    		$site_config = unserialize($site_config['value']);
    		//返回信息
    		$myCenter = $site_config;
			$this->apiSuccess($myCenter);
    }
    /**
     * 用户反馈
     * @param  string $id [description]
     * @return [type]     [description]
     */
    public function fankui($id = '1')
    {
    	if($id){
			if ($this->request->isPost()) {
			    $data            = $this->request->param();
			    if($data['content']){
			    	//所有用户信息
			    	$user_list = $this->user_model->field(['id','name','phone'])->where('status',1)->select();
			    	//将所有用户信息以ID为索引的形式存储
		    		foreach($user_list as $ulist){
		    			$user_data[$ulist['id']] = $ulist;
		    		}
		    		//用户数据
		    		$data['userID'] = $id;
		    		$data['userName'] = $user_data[$id]['name'];
		    		$data['userPhone'] = $user_data[$id]['phone'];
		    		$data['feedbackContent'] = $data['content'];
			    	if ($this->userfeedbacks_model->allowField(true)->save($data)) {
			    	    $this->apiSuccess('','保存成功');
			    	} else {
			    	    $this->apiError('保存失败');
			    	}
			    }else{
			    	$this->apiError('无提交内容');
			    }
			}else{
				$this->apiError('提交方式有误');
			}
		}else{
			$this->apiError('请求错误');
		}
    }
    /**
     * 个人信息
     * @param  string $id [description]
     * @return [type]     [description]
     */
    public function oneself($id = '')
    {
		if($id){
			//用户信息
			$user = $this->user_model->field(['password','status','id'],true)->where('status',1)->where('id',$id)->find();
			if ($this->request->isPost()) {
				$data = $this->request->param();
				if($data){
					if ($this->user_model->isUpdate(true)->allowField(['name','headImg','sex','feteday','site'])->save($data,['id'=>$id]) !== false) {
	                    $this->apiSuccess('','更新成功');
	                    return;
	                } else {
	                    $this->apiSuccess('更新失败');
	                    return;
	                }
				}else{
					$this->apiSuccess('无传输数据');
	                return;
				}
			}else{
				//最近帖子
				$article_time = $this->article_model->where('uid',$id)->order('create_time DESC')->value('create_time');
				$user['article_time'] = $article_time;
				$myCenter = $user->toArray();
				$this->apiSuccess($myCenter);
			}
		}else{
			$this->apiError('请求错误');
		}
    }
}